<?php

namespace app\api\controller;

use app\common\controller\Api;

use think\Db;
use app\admin\model\CoursePush;
use app\admin\model\UserCourse;


/**
 * 会员接口
 */
class Course extends Api
{
    protected $noNeedLogin = ['plan','courseList','coursePush','courseInfo','coachList','planList','getTime','getType','getDate','setPlan','getStatus'];
    protected $noNeedRight = '*';

    /**
     * 获取最新的课程推广
     */
    public function coursePush()
    {
        $this->checkParam('id');
        $id = $this->request->request('id');
        $ret = CoursePush::where('venue_id', $id)->limit(1)->order("weigh desc")->where('deletetime',null)->where('status',0)->select();
        if ($ret) {
            $this->success('推广课程', $ret);
        } else {
            $this->error(__('暂无课程'));
        }
    }

    /*
     * 课程列表
     * */
    public function courseList()
    {
        $id = $this->request->request('id');
        $list = Db::table('keep_course')
            ->alias('c')
            ->join('keep_term t','c.id = t.course_id','left')
            ->join('keep_venue v','v.id = t.venue_id','left')
            ->where('c.venue_id',$id)
            ->order("c.weigh desc")
            ->where('c.deletetime',null)
            ->where('c.status','0')
            ->where('t.type','0')
            ->field('t.stime, v.name as venueName,c.id as course_id, c.name as courseName, c.price, c.day,c.image,c.type')
            ->select()
            ->toArray();
        foreach ($list as &$v) {
            $v['image'] = cdnurl($v['image'], true);
        }
        $this->success('课程列表', $list);
    }

    /*
     * 课程详情
     *
     * */
    public function courseInfo()
    {
        $this->checkParam('id');
        $id = $this->request->request('id');
        $info = Db::table('keep_course')
            ->alias('c')
            ->join('keep_term t','c.id = t.course_id','left')
            ->join('keep_venue v','v.id = t.venue_id','left')
            ->where('c.id',$id)
            ->order("c.weigh desc")
            ->where('c.deletetime',null)
            ->where('c.status','0')
            ->where('t.type','0')
            ->field('t.stime, v.name as venueName, v.address, v.latitude, v.longitude, v.address_image, c.id as course_id, c.name as courseName, c.price, c.day,c.image,c.describe,c.user_ids,c.type')
            ->find();
        $info['image'] = cdnurl($info['image'], true);
        $info['address_image'] = cdnurl($info['address_image'], true);

        if ($info['user_ids']) {
            $coach = explode(',',$info['user_ids']);

            $info['coach'] = DB::name('user')->whereIn('id', $coach)->field('id,username,label,score,age,star,image')->select()->toArray();

            foreach ($info['coach'] as &$v) {
                $v['course'] = DB::name('course')->where('find_in_set(' . $v['id'] . ',user_ids)')->where('deletetime',null)->where('status',0)->column('name');
                $v['image'] = cdnurl($v['image'], true);
            }
        }

        $this->success('课程详情', $info);
    }


    /*
     * 课程预约列表
     * */
    public function planList(){
        $uid  = $this->auth->id;
        $users = \app\common\model\User::where('id', $uid)->whereOr('pid', $uid)->column('id');
        $list = Db::table('keep_user_course')
            ->alias('t')
            ->join('keep_course c','c.id = t.course_id','left')
            ->join('keep_venue v','v.id = t.venue_id','left')
            ->whereIn('t.user_id',$users)
            ->where('t.type','0')
            ->field('t.id as plan_id, v.name as venueName,c.id as course_id, c.name as courseName,  c.day,c.image,c.type')
            ->select()
            ->toArray();
        if ($list) {
            foreach ($list as &$v) {
                $v['image'] = cdnurl($v['image'], true);
            }
        }

        $this->success('课程预约', $list);
    }

    /*
     * 获取课程可预约教练
     * */
    public function coachList()
    {
        $this->checkParam('id');
        $uid = $this->auth->id;
        $plan_id = $this->request->request('id');

        $users = \app\common\model\User::where('id', $uid)->whereOr('pid', $uid)->column('id');
        $userCourse = UserCourse::where('id', $plan_id)->whereIn('user_id', $users)->where('type', '0')->find();
        if (!$userCourse) {
            $this->error('暂无课程需要预订');
        }
        $list = Db::table('keep_term')
            ->alias('t')
            ->distinct('t.user_id')
            ->join('keep_user u','u.id = t.user_id','left')
            ->where('t.course_id',$userCourse['course_id'])
            ->where('t.type','0')
            ->field('u.id,u.username')
            ->select()
            ->toArray();
        $this->success('课程可预约教练', $list);
    }

    /*
     *
     * 可预约时间
     * */
    public function getTime()
    {
        $this->checkParam(['id','uid']);

        $uid = $this->auth->id;
        $plan_id = $this->request->request('id');
        //教练id
        $user_id = $this->request->request('uid');

        $users = \app\common\model\User::where('id', $uid)->whereOr('pid', $uid)->column('id');
        $userCourse = UserCourse::where('id', $plan_id)->whereIn('user_id', $users)->where('type', '0')->find();
        if (!$userCourse) {
            $this->error('暂无课程需要预订');
        }
        $list = Db::table('keep_term')
            ->alias('t')
            ->join('keep_user u','u.id = t.user_id','left')
            ->join('keep_time m','t.time_id = m.id','left')
            ->where('t.course_id',$userCourse['course_id'])
           ->where('t.user_id', $user_id)
            ->where('t.type','0')
            ->field('m.id as time_id, m.stime,m.etime')
            ->select()
            ->toArray();
        $this->success('可预约时间段', $list);
    }

    /*
     * 获取可预约日期
     * */
    public function getDate()
    {
        $this->checkParam(['id','uid', 'time_id']);

        $uid = $this->auth->id;
        $plan_id = $this->request->request('id');
        //教练id
        $user_id = $this->request->request('uid');
        //时间段id
        $time_id = $this->request->request('time_id');

        $users = \app\common\model\User::where('id', $uid)->whereOr('pid', $uid)->column('id');
        $userCourse = UserCourse::where('id', $plan_id)->whereIn('user_id', $users)->where('type', '0')->find();
        if (!$userCourse) {
            $this->error('暂无课程需要预订');
        }
        $list = Db::table('keep_term')
            ->alias('t')
            ->join('keep_user u','u.id = t.user_id','left')
            ->join('keep_time m','t.time_id = m.id','left')
            ->where('t.course_id',$userCourse['course_id'])
            ->where('t.user_id', $user_id)
            ->where('t.time_id', $time_id)
            ->where('t.type','0')
            ->field('t.id as term_id, t.stime,t.etime,t.num')
            ->select()
            ->toArray();
        foreach ($list as &$v) {
            $v['enroll'] = UserCourse::where('term_id', $v['term_id'])->where('type', '1')->where('status', '0')->count();
        }
        $this->success('可预约时间段', $list);
    }

    /*
     *
     * 确定预约
     * */
    public function setPlan()
    {
        $this->checkParam(['id', 'term_id','user_id']);
        $uid = $this->auth->id;
        //用户拥有的课程id
        $plan_id = $this->request->post('id');
        //预约学期id
        $term_id = $this->request->post('term_id');
        //预约学员id
        $user_id = $this->request->post('user_id');



      /*  $users = \app\common\model\User::where('id', $uid)->whereOr('pid', $uid)->column('id');
        $userCourse = UserCourse::where('id', $plan_id)->whereIn('user_id', $users)->where('type', '0')->find();
        if (!$userCourse) {
            $this->error('暂无课程需要预订');
        }*/
        //判断是否有重复预约的
        $userPlan = UserCourse::where('id', $plan_id)->where('user_id', $user_id)->find();

        if ($userPlan['type'] == 1) {
            $this->error('该学员已经预约过本次日期时间段的课程');
        }

        $type = DB::name('course')->where('id', $userPlan['course_id'])->value('type');
        if ($type == 3) {
            $this->error('团体课无法自己预订，预订请联系售后');
        }

        $ret = UserCourse::where('id', $plan_id)->update([
            'user_id' => $user_id,
            'term_id'=>$term_id,
            'type' => 1
            ]);
        if ($ret) {
            $this->success('预约成功,开课前一天允许取消');
        } else {
            $this->error('预约失败，请稍后再试');
        }
    }

    /*
     * 课程类型
     * */
    public function getType()
    {
        $list = array(
            [
                'type' => 0,
                'name' => '大班课'
            ],
            [
                'type' => 1,
                'name' => '私教课'
            ],
            [
                'type' => 2,
                'name' => '免费新课'
            ],
            [
                'type' => 3,
                'name' => '团体课'
            ]
        );
        $this->success('课程类型', $list);
    }

    /*
    * 课程类型
    * */
    public function getStatus()
    {
        $list = array(
            [
                'status' => 0,
                'name' => '待预约'
            ],
            [
                'status' => 1,
                'name' => '待开始'
            ],
            [
                'status' => 2,
                'name' => '进行中'
            ],
            [
                'status' => 3,
                'name' => '已结束'
            ]
        );
        $this->success('课程状态', $list);
    }
}
